<template>
  <div>
    <van-nav-bar fixed title="地址编辑" left-arrow @click-left="$router.go(-1)" />
      <div class="addressEdit">
        <van-address-edit
          :address-info="addressInfo"
          :area-list="areaList"
          show-postal
          show-delete
          show-set-default
          show-search-result
          :search-result="searchResult"
          :area-columns-placeholder="['请选择', '请选择', '请选择']"
          @save="onSave"
          @delete="onDelete"
          @change-detail="onChangeDetail"
          @change-area="onChangeArea"
        />
      </div>
  </div>
</template>

<script>
// import { getProvinceCityArea } from '@/api/address.js'
// import { getAddress, setAddress } from '@/utils/storage'

import { areaList } from '@vant/area-data'
import { addAddress, getAddressDetail, delAddress } from '@/api/address'
export default {
  name: 'addressEdit',
  data () {
    return {
      areaList,
      searchResult: [],
      addressId: this.$route.query.addressId || '',
      addressInfo: {}
    }
  },
  async created () {
    // 数据回显
    this.dataEcho()
  },
  methods: {
    async onSave (content) {
      console.log(content)
      const res = await addAddress(content.name, content.tel, content.addressDetail)
      this.$router.push('/addresslist')
      console.log(res)
      this.$toast('save')
    },
    onDelete () {
      delAddress(this.addressId)
      this.$toast('delete')
      this.$router.push('/addresslist')
    },
    onChangeArea (value) {
      console.log(value)
    },
    onChangeDetail (val) {
      if (val) {
        this.searchResult = [
          {
            name: '黄龙万科中心',
            address: '杭州市西湖区'
          }
        ]
      } else {
        this.searchResult = []
      }
    },
    // 数据回显
    async dataEcho () {
      if (this.addressId) {
        const { data: { detail } } = await getAddressDetail(this.addressId)
        this.addressInfo = {
          id: detail.address_id,
          name: detail.name,
          tel: detail.phone,
          province: detail.region.province,
          city: detail.region.city,
          county: detail.region.region,
          addressDetail: detail.detail
        }
      // console.log(res)
      }
    }
  }

}
</script>

<style lang="less" scoped>
.addressEdit {
  padding-top: 46px;
}
</style>
